package com.cdu.controller;

import com.cdu.pojo.dto.FavoriteDTO;
import com.cdu.pojo.entity.Favorite;
import com.cdu.pojo.vo.FavoriteVO;
import com.cdu.service.FavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
@RestController
@RequestMapping("/api/favorites")
public class FavoriteController {

    @Autowired
    private FavoriteService favoriteService;

    @PostMapping("/add")
    public ResponseEntity<?> addFavorite(@Validated FavoriteVO favoriteVO) {
        // 打印接收到的 FavoriteVO 对象信息
        System.out.println("Received FavoriteVO: " + favoriteVO);

        // 将 FavoriteVO 转换为 FavoriteDTO
        FavoriteDTO favoriteDTO = new FavoriteDTO();
        favoriteDTO.setUserId(favoriteVO.getUserId());
        favoriteDTO.setProductId(favoriteVO.getProductId());

        // 打印转换后的 FavoriteDTO 对象信息
        System.out.println("Converted FavoriteDTO: " + favoriteDTO);

        // 调用服务层方法添加收藏
        if (!favoriteService.addFavorite(favoriteDTO)) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("收藏失败");
        }

        return ResponseEntity.ok("收藏成功");
    }

    @DeleteMapping("/remove/{id}")
    public ResponseEntity<?> removeFavorite(@PathVariable Integer id) {
        if (!favoriteService.removeFavorite(id)) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("取消收藏失败");
        }

        return ResponseEntity.ok("取消收藏成功");
    }

    @GetMapping("/check/{userId}/{productId}")
    public ResponseEntity<?> checkFavorite(@PathVariable Integer userId, @PathVariable Integer productId) {
        System.out.println("Received Integer userId: " + userId + ", productId: "+ productId);
        FavoriteDTO favorite = favoriteService.checkFavoriteExists(userId, productId);
        if (favorite != null) {
            return ResponseEntity.ok(favorite);
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}